﻿using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading;
using vite_core.EF_Core.EFDbcontext;
using vite_entity.Entities;
using vite_net.Common;
using static vite_net.Controllers.User.Delete.Model;

namespace vite_net.Controllers.User.Delete
{
    public class DeleteEndpoint : Endpoint<Request, Result<Response>>
    {
        public readonly ViteNetDbContext _context;

        public DeleteEndpoint(ViteNetDbContext context)
        {
            _context = context;
        }

        public override void Configure()
        {
            Post("/user/delete");
            Roles("ADMIN");
        }

        public override async Task HandleAsync(Request r, CancellationToken c)
        {
            var dbSet = _context.Set<SysUser>();
            await dbSet.AsNoTracking().Where(x => r.UserIds.Contains(x.Id)).ExecuteDeleteAsync(c);
            await _context.SaveChangesAsync();
            await SendAsync(Result<Response>.Ok(new Model.Response()
            {
                Code = 1,
                Msg = "一切ok"
            }));
        }
    }
}
